<?php

namespace app\proxy\controller\product;

use app\proxy\controller\Controller;
use app\proxy\model\product\ProxyCategory as CategoryModel;
use app\proxy\model\proxy\User as ProxyUser;
use app\shop\model\product\Category as ParentCategory;

/**
 * 商品分类
 */
class ProxyCategory extends Controller
{
    /**
     * 商品分类列表
     */
    public function index()
    {
        $model   = new CategoryModel;
        $param   = $this->postData();
        $regions = $this->getRegionIds();
        $clist    = $model->getProxyALL([], $param['region_id'], $this->getProxyId())['all'];

        $category_count = [];
        foreach ($regions as $gvalue) {
            $category_count[] = $model->getProxyTotal(['audit_status' => 1], $gvalue, $this->getProxyId());
        }
        
        $ParentCategory = new ParentCategory;
        $plist = $ParentCategory->where("parent_id=0")->field('*,0 as can_edit')->with(['image'])->order(['sort' => 'asc', 'category_id' => 'asc',  'create_time' => 'asc'])->select();

        $plist = $plist->toArray();
        $list = array_merge($clist, $plist);
        $list = get_tree_children($list, 'children', 'category_id', 'parent_id');

        //分类列表
        $ParentCategory = new ParentCategory;
        $category = $ParentCategory->field('category_id as value,parent_id as pid,name as label')->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
        
        if (!$category->isEmpty()) {
            $category = $category->toArray();
            $category = get_tree_children($category, 'children', 'value');
        }

        return $this->renderSuccess('', compact('list', 'category_count', 'category'));
    }

    /**
     * 商品分类列表
     */
    public function adminindex()
    {
        $model   = new CategoryModel;
        $param   = $this->postData();
        $regions = $this->getRegionIds();
        $list    = $model->getProxyALL($param, $regions, $this->getProxyId())['all'];
        // 数量
        $category_count = [
            'using'   => $model->getProxyTotal(['audit_status' => 1], $regions),
            'audit'   => $model->getProxyTotal(['audit_status' => 0], $regions),
            'no_pass' => $model->getProxyTotal(['audit_status' => 2], $regions),
        ];
        //分类列表
        $model    = new \app\shop\model\product\Category;
        $category = $model->field('category_id as value,parent_id as pid,name as label')->order(['sort' => 'asc', 'create_time' => 'asc'])->select();

        if (!$category->isEmpty()) {
            $category = $category->toArray();
            $category = get_tree_children($category, 'children', 'value');
        }

        return $this->renderSuccess('', compact('list', 'category_count', 'category'));
    }

    /**
     * 添加商品分类
     */
    public function add()
    {
        $nosh = false;
        $proxyUser = ProxyUser::detail($this->getProxyId());
        if($proxyUser&&isset($proxyUser['authority'])){
            $authority = json_decode($proxyUser['authority'],true);
            if(isset($authority['category'])&&$authority['category']==1){
                $nosh = true;
            }
        }
        $model = new CategoryModel;
        // 新增记录
        if ($model->add(array_merge($this->postData(), $nosh?['audit_status' => 1]:['audit_status' => 0]), $this->getProxyId())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑商品分类
     */
    public function edit($id)
    {
        $nosh = false;
        $proxyUser = ProxyUser::detail($this->getProxyId());
        if($proxyUser&&isset($proxyUser['authority'])){
            $authority = json_decode($proxyUser['authority'],true);
            if(isset($authority['category'])&&$authority['category']==1){
                $nosh = true;
            }
        }
        // 模板详情
        $model = CategoryModel::detail($id);
        if (!$model || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('商品分类不存在');
        }
        // 更新记录
        if ($model->edit(array_merge($this->request->post(), $nosh?['audit_status' => 1]:['audit_status' => 0]), $this->getProxyId())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除商品分类
     */
    public function delete($id)
    {
        $model = CategoryModel::detail($id);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('商品分类不存在');
        }
        if ($model->remove($id)) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

    /**
     * 得到修改图片
     * @return array
     */
    public function image($id)
    {
        $model  = new CategoryModel;
        $detail = $model->with(['image'])->where(['id' => $id])->find();
        return $this->renderSuccess('', compact('detail'));
    }

}
